# 将重复的oracle建表语句按照单个表生成对应文件
# 不能重复执行
# 再次执行时需要将之前生成的文件删除
import re
import os
import sys

filePath = sys.argv[1]  # 文件路径  python TableSplit.py D:\test\txt\uu\create.sql
fileEncoding = "GBK"   # 文本编码格式

# 判断生成的表名列表对应的文件夹是否存在
if not os.path.isfile(filePath):
    print("文件路径不存在！！！")

with open(filePath, "r", encoding=fileEncoding) as fo:
    for fileText in fo:
        if "create table" in fileText:
            rep = re.compile("[a-zA-Z]*\.[\w]*")
            a = "".join(rep.findall(fileText))  # 获取表名
        with open(os.path.split(filePath)[0] + "\\" + a + ".sql", "a", encoding=fileEncoding) as fo1:
            fo1.write(fileText)
